<?php
namespace app\bancard\controller;

use think\Db;
use think\facade\Cache;

class Attendancegrade extends Common
{

    public $attendance_status = [0=>'',1 => '正常', 2 => '迟到', 3 => '早退', 4 => '缺卡', 5 => '请假'];

    //年级考勤首页
    public function index()
    {
        $grade_arr_info=$this->getGrade();
        $this->assign('grade_arr', $grade_arr_info['data']);
        return $this->fetch();
    }

    //年级考勤设置列表
    public function settingLists()
    {
        $limit = request()->param('limit');
        $offset = request()->param('offset');
        $start = ($offset - 1) * $limit;

        $map=['type'=>2,'school_id'=>session('schoolInfo.id'),'status'=> 1];

        $ntitle = request()->param('keywords');
        $title = isset($ntitle) ? $ntitle : '';


        if ($title) {

            $where=['grade_id'=>$title];

            $count = db('attendance_settings')->where($map)->where($where)->count();
            $result = db('attendance_settings')->where($map)->where($where)->limit($start, $limit)->order('start_time asc,end_time asc')->select();

        }else{
            $count = db('attendance_settings')->where($map)->count();
            $result = db('attendance_settings')->where($map)->limit($start, $limit)->order('start_time asc,end_time asc')->select();
        }


        $grade_arr_info=$this->getGrade();
        $grade_arr = array_column($grade_arr_info['data'], 'name', 'id');


        foreach($result as $key=>$value){
            $result[$key]['grade_name']=isset($grade_arr[$value['grade_id']])?$grade_arr[$value['grade_id']]:'';
        }

        return showTable(0, 'OK', $count, $result, 200);
    }

    //某个年级考勤列表
    public function view()
    {
        $data = request()->param();
        $result = $this->getClassesByGradeIds($data['id']);

        $this->assign('class_arr',$result['data']);

        return $this->fetch();

    }

    //年级考勤，某个年级下的考勤详细信息数据
    public function lists()
    {

        $grade_id = request()->param('grade_id');
        $event_id = request()->param('event_id');
        //没有年级——id则返回错误
        if(empty($grade_id)){
            return show(0, '查询失败！非法操作！！', 0, 200);
        }

        $map = ['a.grade_id'=>$grade_id,'a.school_id'=>session('schoolInfo.id'),'a.event_id'=>$event_id];

        $ntitle = request()->param('keywords');
        $title = isset($ntitle) ? $ntitle : '';

        $limit = request()->param('limit');
        $offset = request()->param('offset');
        $start = ($offset - 1) * $limit;

        if ($title) {

            $where=['a.class_id'=>$title];

            $count = db('attendance_grade_log')->alias('a')->where($map)->where($where)->count();
            $result = db('attendance_grade_log')->alias('a')->leftjoin('attendance_settings b', 'a.event_id=b.id')->where($map)->where($where)->limit($start, $limit)->order('a.id desc')->select();


        } else {
            $count = db('attendance_grade_log')->alias('a')->where($map)->count();
            $result = db('attendance_grade_log')->alias('a')->leftjoin('attendance_settings b', 'a.event_id=b.id')->where($map)->limit($start, $limit)->order('a.id desc')->select();

        }

        if(empty($result)){
            return showTable(0, 'OK', $count, $result, 200);
        }

        $student_id = array_column($result,'student_id');
        $class_ids = array_column($result,'class_id');

        $d = $this->getStudents($student_id);
        $students  = $d['data'];
        $c = $this->getClasses($class_ids);
        $class = $c['data'];
        foreach ($result as $key=>$value){
            if(isset($students[$value['student_id']])){
                $result[$key]['student_name'] = $students[$value['student_id']]['name'];
                $result[$key]['student_number'] = $students[$value['student_id']]['number'];
                $result[$key]['student_avatar'] = $students[$value['student_id']]['avatar'];
            }
            if(isset($class[$value['class_id']])){
                $result[$key]['classroom_name'] = $class[$value['class_id']]['classroom_name'];
                $result[$key]['building_name'] = $class[$value['class_id']]['building_name'];
                $result[$key]['class_name'] = $class[$value['class_id']]['name'];
            }
            $result[$key]['status'] = $this->attendance_status[$value['status']];
        }

        return showTable(0, 'OK', $count, $result, 200);
    }


    //添加
    public function add()
    {

        $grade_arr_info=$this->getGrade();

        $this->assign('grade_arr', $grade_arr_info['data']);
        return $this->fetch();
    }

    //编辑
    public function edit()
    {

        $grade_arr_info=$this->getGrade();
        $this->assign('grade_arr', $grade_arr_info['data']);

        $data = request()->param();
        $result = db('attendance_settings')->where('id', '=', $data['id'])->find();
        $this->assign('result', $result);
        return $this->fetch();
    }



}